package com.luo.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.ams.mapper.AdminMapper;
import com.luo.ams.mapper.AdminRoleMapper;
import com.luo.ams.mapper.RoleMapper;
import com.luo.ams.mapper.RolePermissionMapper;
import com.luo.ams.service.IRoleService;
import com.luo.common.exception.BBSServiceException;
import com.luo.common.restful.ResponseCode;
import com.luo.pojo.ams.dto.RoleAddDTO;
import com.luo.pojo.ams.entity.Admin;
import com.luo.pojo.ams.entity.AdminRole;
import com.luo.pojo.ams.entity.Role;
import com.luo.pojo.ams.entity.RolePermission;
import com.luo.pojo.ams.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author bin.cn
 * @since 2023-01-19
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Transactional
    @Override
    public void addNew(RoleAddDTO roleAddDTO) {
        if (roleAddDTO.getName()==null||roleAddDTO.getDescription()==null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST,"参数不能为空");
        }
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("name",roleAddDTO.getName());
        Role role = roleMapper.selectOne(wrapper);
        if (role!=null){
            throw new BBSServiceException(ResponseCode.CONFLICT,"此角色已存在");
        }
        role = new Role();
        BeanUtils.copyProperties(roleAddDTO,role);
        roleMapper.insert(role);
        log.debug("添加角色成功！");

        //添加角色，为角色分配权限
        log.debug("分配权限");
        Long[] permissionIds = roleAddDTO.getPermissionIds();
        int length = permissionIds.length;
        RolePermission[] rolePermissions = new RolePermission[length];
        for (int i = 0; i < length; i++){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permissionIds[i]);
            rolePermission.setRoleId(role.getId());
            rolePermissions[i] = rolePermission;
        }
        rolePermissionMapper.insertBatch(rolePermissions);

    }

    @Override
    public RoleVo getDetailById(Integer id) {
        RoleVo detailById = roleMapper.getDetailById(id);
        return detailById;
    }

    @Transactional
    @Override
    public void updateByRoleId(RoleAddDTO roleAddDTO) {
        if (roleAddDTO.getDescription()==null || roleAddDTO.getSort()==null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST,"参数不能为空");
        }
        //修改角色表
        Role role = new Role();
        role.setDescription(roleAddDTO.getDescription());
        role.setSort(role.getSort());
        role.setId(roleAddDTO.getId());
        roleMapper.updateById(role);
        //删除关联表数据，重新插入
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",role.getId());
        rolePermissionMapper.delete(wrapper);

        Long[] permissionIds = roleAddDTO.getPermissionIds();
        int length = permissionIds.length;
        RolePermission[] rolePermissions = new RolePermission[length];
        for (int i = 0; i < length; i++) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(permissionIds[i]);
            rolePermissions[i] = rolePermission;
        }
        rolePermissionMapper.insertBatch(rolePermissions);
    }

    @Transactional
    @Override
    public void deleteById(Long id) {
        if (id==null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST,"参数不能为空");
        }
        Role role = roleMapper.selectById(id);
        //不存在不允许删除
        if (role==null){
            throw new BBSServiceException(ResponseCode.NOT_FOUND,"管理员不存在");
        }

        //有管理员绑定，不能删除角色
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("description",role.getName());
        List<Admin> admins =
                adminMapper.selectList(adminQueryWrapper);
        if (admins.size() > 0){
            throw new BBSServiceException(ResponseCode.CONFLICT,"无法删除");
        }

        roleMapper.deleteById(id);


        QueryWrapper<AdminRole> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_id",id);
        adminRoleMapper.delete(roleQueryWrapper);


        QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
        rolePermissionQueryWrapper.eq("role_id",id);
        rolePermissionMapper.delete(rolePermissionQueryWrapper);

    }
}
